package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapShader
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.Shader
import android.util.AttributeSet
import android.view.View
import androidx.core.graphics.applyCanvas
import com.android.kotlincoroutinemvvmdemo.R

class ReflectView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null) :
    View(context, attributeSet) {

    private val textContent = "Hello,World!"

    private var mWidth = 0
    private var mHeight = 0

    private var mBitmap: Bitmap? = null

    private var mBitmapShader: BitmapShader? = null

    private val textBound by lazy {
        Rect()
    }

    private val reflectRectF by lazy {
        RectF()
    }

    //private val mMatrix by lazy {
    //    Matrix()
    //}

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            textSize = context.resources.getDimension(R.dimen.sp_20)
            style = Paint.Style.FILL
            color = context.resources.getColor(R.color.color_e0eb3b)
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_200).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        //绘制倒影思路:在onDraw()中得到canvas，将其转化为Bitmap，再创建Bitmap的倒影，最后将Bitmap倒影重绘到canvas上。
        super.onDraw(canvas)
        setBackgroundColor(resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            mPaint.getTextBounds(textContent, 0, textContent.length, textBound)
            val fontHeight = mPaint.fontMetrics.bottom - mPaint.fontMetrics.top

            if (mBitmap == null) {
               mBitmap = Bitmap.createBitmap(mWidth, mHeight / 2, Bitmap.Config.ARGB_8888)
            }

            val newBitmap = mBitmap?.applyCanvas {
                val newBmpWidth = mWidth / 2
                val newBmpHeight = mHeight / 2
                val textX0 = newBmpWidth - textBound.width() / 2.0F
                val textY0 = newBmpHeight - (newBmpHeight - fontHeight) / 2 - mPaint.fontMetrics.bottom
                drawText(textContent, textX0 , textY0, mPaint)
            }

            drawBitmap(newBitmap!!, 0F, 0F, null)
            //mMatrix.setTranslate(0F, 0F)
            //drawBitmap(newBitmap!!, mMatrix, null)

            if (mBitmapShader == null) {
                mBitmapShader =
                    BitmapShader(newBitmap, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR)
            }

            reflectRectF.set(
                0F,
                mHeight / 2.0F,
                mWidth.toFloat(),
                mHeight.toFloat()
            )
            mPaint.shader = mBitmapShader
            drawRect(reflectRectF, mPaint)
        }
    }
}